package com.springboot_shopping.controller;

import com.springboot_shopping.entity.UserAddress;
import com.springboot_shopping.service.UserAddressService;
import com.springboot_shopping.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/address")
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    // 获取当前用户的所有地址
    @GetMapping("/list")
    public Result<List<UserAddress>> list(HttpSession session,HttpServletRequest request) {
        Integer userId = Integer.parseInt(request.getHeader("userId"));
//        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Result.error("未登录");
        }

        List<UserAddress> addressList = userAddressService.findByUserId(userId);
        return Result.success(addressList);
    }

    // 根据标签筛选地址
    @GetMapping("/filter")
    public Result<List<UserAddress>> filter(@RequestParam String label, HttpSession session,HttpServletRequest request) {
        Integer userId = Integer.parseInt(request.getHeader("userId"));
        if (userId == null) {
            return Result.error("未登录");
        }

        List<UserAddress> addressList = userAddressService.findByLabelAndUserId(label, userId);
        return Result.success(addressList);
    }

    // 获取地址详情
    @GetMapping("/{id}")
    public Result<UserAddress> detail(@PathVariable Integer id, HttpSession session,HttpServletRequest request) {
        Integer userId = Integer.parseInt(request.getHeader("userId"));
        if (userId == null) {
            return Result.error("未登录");
        }

        UserAddress address = userAddressService.findById(id);
        if (address == null || !address.getUserId().equals(userId)) {
            return Result.error("地址不存在");
        }

        return Result.success(address);
    }



    // 设置默认地址
    @PutMapping("/default/{id}")
    public Result<String> setDefault(@PathVariable Integer id, HttpSession session,HttpServletRequest request) {
        Integer userId = Integer.parseInt(request.getHeader("userId"));
        if (userId == null) {
            return Result.error("未登录");
        }

        boolean result = userAddressService.setDefaultAddress(id, userId);

        if (result) {
            return Result.success("设置默认地址成功");
        } else {
            return Result.error("设置默认地址失败");
        }
    }

    // 新增地址
    @PostMapping("/add")
    public Result<String> add(@RequestBody UserAddress address, HttpSession session,HttpServletRequest request) {
        Integer userId = Integer.parseInt(request.getHeader("userId"));
        System.out.println(userId);
        if (userId == null) {
            return Result.error("未登录");
        }
        System.out.println(address.toString());
        address.setUserId(userId);
        boolean result = userAddressService.addAddress(address);

        if (result) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    // 修改地址
    @PutMapping("/update")
    public Result<String> update(@RequestBody UserAddress address, HttpSession session,HttpServletRequest request) {
        Integer userId = Integer.parseInt(request.getHeader("userId"));
        if (userId == null) {
            return Result.error("未登录");
        }

        UserAddress dbAddress = userAddressService.findById(address.getId());
        if (dbAddress == null || !dbAddress.getUserId().equals(userId)) {
            return Result.error("地址不存在");
        }

        address.setUserId(userId);
        boolean result = userAddressService.updateAddress(address);

        if (result) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

    // 删除地址
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Integer id, HttpSession session,HttpServletRequest request) {
        Integer userId = Integer.parseInt(request.getHeader("userId"));
        if (userId == null) {
            return Result.error("未登录");
        }

        boolean result = userAddressService.deleteAddress(id, userId);

        if (result) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }
}